package hot100;

public class trap2 {
    public int trap(int[] height) {
        int n = height.length;
        int maxL = 0, maxR = 0;
        int sumL = 0, sumR = 0, sumHeight = 0;
        for (int l = 0; l < n; l++) {
            maxL = height[l] > maxL ? height[l] : maxL;
            sumL += maxL;
            sumHeight += height[l];
        }
        for (int r = n - 1; r >= 0; r--) {
            maxR = height[r] > maxR ? height[r] : maxR;
            sumR += maxR;
        }
        return sumL + sumR - maxL * n - sumHeight;
    }
}
